﻿using Newtonsoft.Json.Linq;
using OpenCvSharp;
using RPAVIEW.util;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace RPAVIEW.views.HomePages {
    /// <summary>
    /// TriggerPage.xaml 的交互逻辑
    /// </summary>
    public partial class TriggerPage:UserControl {
        public static readonly DependencyProperty homeProperty = DependencyProperty.Register(
            "Home",
            typeof(MainWindow),
            typeof(TriggerPage),
            new PropertyMetadata(null, null));
        public MainWindow Home{
            get { return (MainWindow)GetValue(homeProperty);}
            set { SetValue(homeProperty, value);}
        }
        public TriggerPage() {
            InitializeComponent();
        }

        private void SelectTriggerType(object sender, RoutedEventArgs e) {
            if(this.pop.Visibility.Equals(Visibility.Collapsed)) { 
                this.pop.Visibility = Visibility.Visible;
            } else { 
                this.pop.Visibility = Visibility.Collapsed;
            }
        }

        private void TimeTrigger(object sender, RoutedEventArgs e) {
            this.pop.Visibility = Visibility.Collapsed;
            MainWindow mainWindow = WindowUtil.GetWindow<MainWindow>();
            List<ProcessInfo> processes = mainWindow.GetUserProcesses();
            TriggerInfo triggerInfo = new TriggerInfo();
            triggerInfo.Owner = mainWindow;
            triggerInfo.app.ItemsSource = processes;
            triggerInfo.parentPage = this;
            Trigger<TimeTriggerEntry> trigger = new Trigger<TimeTriggerEntry>();
            trigger.accountId = (Application.Current as App).currentUser.accountId;
            triggerInfo.DataContext = trigger;
            
            TimeTriggerEntry entry = new TimeTriggerEntry();
            trigger.triggerEntry = entry;
            
            TimeTrigger tt = new TimeTrigger();
            tt.DataContext = entry;
            

            triggerInfo.triggerEntryArea.Children.Add(tt);
            triggerInfo.Show();
            e.Handled = true;
        }

        public void UpdateList() { 
            this.triggerList.ItemsSource = Home.GetUserTriggers();
        }

        private void EditFromList(object sender, RoutedEventArgs e) {
            Button currentButton = sender as Button;
            Trigger<TimeTriggerEntry> trigger = (currentButton.Parent as WrapPanel).DataContext as Trigger<TimeTriggerEntry>;
            MainWindow mainWindow = WindowUtil.GetWindow<MainWindow>();
            List<ProcessInfo> processes = mainWindow.GetUserProcesses();
            TriggerInfo triggerInfo = new TriggerInfo();
            trigger.processInfo = processes.FirstOrDefault(p => p.processId.Equals(trigger.processInfo.processId));
            triggerInfo.Owner = mainWindow;
            triggerInfo.app.ItemsSource = processes;
            triggerInfo.parentPage = this;
            triggerInfo.DataContext = trigger;
            TimeTrigger tt = new TimeTrigger();
            TimeTriggerEntry entry = trigger.triggerEntry;
            tt.DataContext = entry;
            triggerInfo.triggerEntryArea.Children.Add(tt);
            triggerInfo.Show();
        }

        private void DeleteFromList(object sender, RoutedEventArgs e) {
            MessageBoxResult re = MessageBox.Show("确定删除该触发器么？", "提示", MessageBoxButton.OKCancel);
            if(MessageBoxResult.OK.Equals(re)) { 
                HttpUtil httpUtil = HttpUtil.getInstance(SystemUtil.GetAPPConfig("ControlBaseIp") + SystemUtil.GetAPPConfig("TriggerUri"));
                Button currentButton = sender as Button;
                Trigger<TimeTriggerEntry> triggerInfo = (currentButton.Parent as WrapPanel).DataContext as Trigger<TimeTriggerEntry>;
                HttpResponse<JObject> res = httpUtil.SendGet<JObject>("/remove", new Dictionary<string, string> { { "triggerId",  triggerInfo.triggerId}, { "entryId", triggerInfo.triggerEntry.triggerEntryId } });
                if(res.Code == 500) {
                    MessageBox.Show("删除失败");
                } else { 
                    MessageBox.Show("删除成功");
                    ScheduleUtil.DeleteTrigger(triggerInfo);
                    this.triggerList.ItemsSource = Home.GetUserTriggers();
                }
            }
        }

        private void SwitchEnable(object sender, RoutedEventArgs e) {
            ToggleButton enableButton = sender as ToggleButton;
            Trigger<TimeTriggerEntry> triggerInfo = (enableButton.Parent as WrapPanel).DataContext as Trigger<TimeTriggerEntry>;
            HttpUtil httpUtil = HttpUtil.getInstance(SystemUtil.GetAPPConfig("ControlBaseIp") + SystemUtil.GetAPPConfig("TriggerUri"));
            HttpResponse<JObject> res = httpUtil.SendGet<JObject>("/switchEnable", new Dictionary<string, string> {{ "triggerId",  triggerInfo.triggerId}, { "status", (!triggerInfo.enable).ToString() } });
            if(res.Code != 500) { 
                triggerInfo.enable = !triggerInfo.enable;
                if(triggerInfo.enable ?? false) {
                    ScheduleUtil.UpdateTrigger(triggerInfo, WindowUtil.GetWindow<MainWindow>());
                } else { 
                    ScheduleUtil.DeleteTrigger(triggerInfo);
                }
            } else { 
                triggerInfo.enable = triggerInfo.enable;
            }
        }
    }
}
